﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;

namespace mamnontuoitho7.module
{
    public partial class ChangeAvatar : System.Web.UI.Page
    {
        protected Member _member = new Member();
        protected General _general = new General();
        private ThumbnailClass _thumbnailClass = new ThumbnailClass();
        private Class _class = new Class();
        protected DataRow _dtrMember;

        protected void Page_Load(object sender, EventArgs e)
        {

            //Check Permission

            if (Session["memberID"].ToString() != "")
            {
                try
                {
                    imgMember.ImageUrl = "../images/avatar/teacher_parents/" + (Session["avatar"].ToString() == "" ? ConstantClass._memberDefaultAvatar : Session["avatar"].ToString());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorLoginPage);
                }
            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }


        }


        protected void btnChangeAvatar_Click(object sender, EventArgs e)
        {
            try
            { 
                string avatar = "";
                if (Session["avatar"].ToString() == "")
                {
                    avatar = Session["fullname"].ToString() + "-" + Session["memberID"].ToString();
                }
                else
                {
                    avatar = Session["avatar"].ToString().Substring(0, Session["avatar"].ToString().LastIndexOf('.'));
                }

                //Check image upload
                if (fulAvatar.FileName != "" && fulAvatar.FileName != null)
                {
                    if (fulAvatar.FileBytes.Count() / 1024 > 6144)
                    {
                        lblNotice.Visible = true;
                        lblNotice.Text = "Dung lượng hình ảnh phải dưới 6MB.";
                        return;
                    }
                    avatar = _thumbnailClass.UploadImage2(fulAvatar, Request.PhysicalApplicationPath + "images/avatar/teacher_parents/", avatar, 128, 128, Session["avatar"].ToString());

                    if (avatar == "")
                    {
                        lblNotice.Visible = true;
                        lblNotice.Text = "Hình của bạn không thể upload được. File hình chỉ hỗ trợ những file: jpg, jpeg, gif, png, bmp, tiff";
                        return;
                    }
                }
                else
                {
                    lblNotice.Visible = true;
                    lblNotice.Text = "Bạn chưa chọn hình upload.";
                    return;
                }

                _member.UpdateAvatar(Session["memberID"].ToString(), avatar);
                Session["avatar"] = avatar;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorLoginPage);
            }
                Response.Redirect("ChangeAvatar.aspx");
                //Response.AddHeader("REFRESH", "0;URL=ChangeAvatar.aspx");

        }

    }
}